package net.xo68.destiny.manager.config;

import net.xo68.destiny.contract.common.SystemConstants;
import net.xo68.destiny.starter.common.SchedulerCommonProperties;
import net.xo68.destiny.starter.manager.SchedulerManagerProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 执行器接口请求拦截器
 * @author wuxie
 * @version 2020-9-29
 */
@Component
public class ApiRequestInterceptor implements ClientHttpRequestInterceptor {

    @Autowired
    private SchedulerManagerProperties schedulerManagerProperties;
    @Autowired
    private SchedulerCommonProperties schedulerCommonProperties;

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        HttpHeaders headers = request.getHeaders();

        // 加入自定义字段
        headers.add(SystemConstants.API_TOKEN_HEADER,SystemConstants.API_TOKEN_PREFIX+schedulerCommonProperties.getApiAccessToken());

        // 保证请求继续被执行
        return execution.execute(request, body);
    }
}
